import { Component, OnInit } from '@angular/core';
import { DialogService, LazyLoadEvent } from 'primeng/primeng';
import { Router, ActivatedRoute } from '@angular/router';
import { HotlineSearchDialogComponent } from './hotline-search-dialog.component';
import { HotlineService } from '../hotline.service';
import { MdlSnackbarService } from '@angular-mdl/core';

@Component({
    templateUrl: './hotline-search.component.html',
    styleUrls: ['./hotline-search.component.scss'],
    providers: [DialogService, HotlineService, MdlSnackbarService]
})
export class HotlineSearchComponent implements OnInit {
    selectedRecord: any = {};
    cols: any[] = [
        { field: 'yesswNum', header: '热线编号', style: { 'width': '150px', 'text-align': 'center' } },
        { field: 'reporterName', header: '举报人', style: { 'width': '100px', 'text-align': 'center' } },
        { field: 'callNum', header: '来电号码', style: { 'width': '150px', 'text-align': 'center' } },
        { field: 'dispatchType', header: '派遣方式/问题来源', style: { 'width': '150px', 'text-align': 'center' } },
        { field: 'receiveUnitName', header: '处置部门', style: { 'width': '100px', 'text-align': 'center' } },
        { field: 'answerFlagName', header: '专业部门反馈', style: { 'width': '100px', 'text-align': 'center' } },
        { field: 'finishFlagName', header: '问题解决', style: { 'width': '100px', 'text-align': 'center' } },
        { field: 'pleasedFlagName', header: '问题处理满意', style: { 'width': '100px', 'text-align': 'center' } },
        { field: 'cityFinishFlagName', header: '问题解决(市级)', style: { 'width': '120px', 'text-align': 'center' } },
        { field: 'cityPleasedFlagName', header: '问题处理满意(市级)', style: { 'width': '120px', 'text-align': 'center' } }
    ];
    searchCriteria: any = {};
    items: any[] = [];
    loading: boolean = true;
    totalRecords: number = 0;

    constructor(
        private router: Router,
        private route: ActivatedRoute,
        public hotlineService: HotlineService,
        public dialogService: DialogService,
        private mdlSnackbarService: MdlSnackbarService
    ) { }

    ngOnInit() {
    }

    public search() {
        this.loading = true;
        let params = this.searchCriteria || {};
        params.pageSize = 30;
        params.pageNum = 1;
        this.hotlineService.search(params).subscribe(items => {
            this.loading = false;
            this.items = items.list;
            this.totalRecords = items.total;
            if (this.items.length > 0) {
                this.selectedRecord = this.items[0];
            }
        });
    }

    /**高级查询 */
    public showSearchDialog($event) {
        const dialog = this.dialogService.open(
            HotlineSearchDialogComponent, {
                header: '高级查询',
                width: '850px',
                data: {
                    'searchCriteria': this.searchCriteria
                },
                contentStyle: { 'height': '450px', 'background-color': '#101E5A', 'border': '1px solid #1A2869' },
                styleClass: "stat-container-dialog"
            });
        dialog.onClose.subscribe(data => {
            if (data) {
                this.searchCriteria = data;
                this.search();
            }
        })
    }
    /** 查询 */
    loadLazy(event: LazyLoadEvent) {
        this.loading = true;
        let params = this.searchCriteria || {};
        params.pageSize = 30;
        params.pageNum = event.first / event.rows + 1;
        this.hotlineService.search(params).subscribe(items => {
            this.loading = false;
            this.items = items.list;
            this.totalRecords = items.total;
            if (this.items.length > 0) {
                this.selectedRecord = this.items[0];
            }
        })
    }
}